﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.Roles;
using MediatR;
using Microsoft.AspNetCore.Http;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.Roles
{
    public class UserRoleCommandHandler : IRequestHandler<UserRoleCommand, APIResult<object>>
    {
        private readonly IBaseRepository<UserRole> userroleRepos;
        private readonly IMapper mapper;

        public UserRoleCommandHandler(IBaseRepository<UserRole> userroleRepos,IMapper mapper)
        {
            this.userroleRepos = userroleRepos;
            this.mapper = mapper;
        }



        public async Task<APIResult<object>> Handle(UserRoleCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            // 将 UserId 列表转换为 UserRole 对象列表（RoleId 固定为 1）
            var userRoles = request.UserIds
                .Select(userId => new UserRole { UserId = userId, RoleId = request.RoleId })
                .ToList();

            // 批量添加并保存
             var res = await userroleRepos.AddRanges(userRoles);
            result.Code = APIEnum.批量添加成功;
            result.Msg = APIEnum.批量添加成功.ToString();
            result.data = res;

            return await Task.FromResult(result);


        }




    }
}
